{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3f166bcf",
   "metadata": {},
   "source": [
    "# 本周知识\n",
    "> 1.函数的返回值  \n",
    "> 2.函数的参数类型  \n",
    "> 3.模块与函数的关系  \n",
    "> 4.定义一个模块，然后调用模块  \n",
    ">> A.调用模块  \n",
    ">> B.调用模块中的函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6328b29",
   "metadata": {},
   "source": [
    "# 1.函数的返回值\n",
    "> 1. 返回值的关键字:return\n",
    "> 2. 在其他的编程语言中，一般return一定要写，不写会报错；但是在python中，return可以不写，return 默认为 None\n",
    ">> 1. 编程语言中的函数，一定有返回值\n",
    ">> 2. 返回值的结果是一个值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "9efd4b8e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_formatted_name(first_name,last_name):\n",
    "    \"\"\"返回整洁的名字\"\"\"\n",
    "    full_name=first_name+''+last_name\n",
    "    # 函数的返回结果为full_name\n",
    "    return full_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "d58c8f48",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lihua\n"
     ]
    }
   ],
   "source": [
    "print(get_formatted_name(\"li\",\"hua\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "17454438",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lihua\n"
     ]
    }
   ],
   "source": [
    "musician=get_formatted_name(\"li\",\"hua\")\n",
    "print(musician)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69998485",
   "metadata": {},
   "source": [
    "# 2. 函数的参数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29036284",
   "metadata": {},
   "source": [
    "### 1. 函数输入的参数\n",
    "\n",
    "> 1. 函数可以输入复杂的参数：  \n",
    ">> 1. 函数参数为列表\n",
    ">> 2. 函数参数为字典 \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "9ca289d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "def list_test(names):\n",
    "    \"\"\"函数输入一个列表\"\"\"\n",
    "    print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "9f698153",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Lihua', 'Ziqi', 'Xiaofeng']\n"
     ]
    }
   ],
   "source": [
    "list_test([\"Lihua\",\"Ziqi\",\"Xiaofeng\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "dd4e0706",
   "metadata": {},
   "outputs": [],
   "source": [
    "def dict_test(request_header):\n",
    "    \"\"\"函数输入一个字典\"\"\"\n",
    "    print(request_header)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "cc1600d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# http://httpbin.org/get\n",
    "httpbin_get = {\n",
    "  \"args\": {}, \n",
    "  \"headers\": {\n",
    "    \"Accept\": \"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\", \n",
    "    \"Accept-Encoding\": \"gzip, deflate\", \n",
    "    \"Accept-Language\": \"zh-CN,zh;q=0.9\", \n",
    "    \"Host\": \"httpbin.org\", \n",
    "    \"Upgrade-Insecure-Requests\": \"1\", \n",
    "    \"User-Agent\": \"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36\", \n",
    "    \"X-Amzn-Trace-Id\": \"Root=1-61ad737f-7f5950b167a70a144d4c082b\"\n",
    "  }, \n",
    "  \"origin\": \"210.21.79.236\", \n",
    "  \"url\": \"http://httpbin.org/get\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "e8a66864",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Host': 'httpbin.org', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36', 'X-Amzn-Trace-Id': 'Root=1-61ad737f-7f5950b167a70a144d4c082b'}\n"
     ]
    }
   ],
   "source": [
    "# 调用函数时，给一个字典作为输入值\n",
    "dict_test(httpbin_get[\"headers\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40532f9f",
   "metadata": {},
   "source": [
    ">  2. 传递任意数量的实参  \n",
    "> 1. * args  \n",
    ">> 1. args 可以接受多个数据（单值）\n",
    ">> 2. args 将多个数据存在一个元组当中，实际还是一个变量\n",
    "> 2. * kwargs\n",
    ">> 1. kwargs 可以接受多个数据(键值对)==> 关键字参数\n",
    ">> 2. kwargs 将多个 键值对 的数据存放在一个 字典 中，实际上还是一个变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "017054b7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('mushroom', 'green peppers', 'extra cheese')"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 回顾元组的定义\n",
    "toppings = \"mushroom\",\"green peppers\",\"extra cheese\"\n",
    "toppings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "445b6828",
   "metadata": {},
   "outputs": [],
   "source": [
    "# *args 实践：\n",
    "def make_food(food,*toppings):  \n",
    "    \"\"\"打印顾客点的所有配料\"\"\"\n",
    "    return food,toppings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "260924f1",
   "metadata": {},
   "outputs": [],
   "source": [
    "build_food = make_food(\"pizza\",\"mushroom\",\"green peppers\",\"extra cheese\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "a3caaee3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('pizza', ('mushroom', 'green peppers', 'extra cheese'))"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "build_food"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "b5f680ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "# **kwargs 实践\n",
    "def person(name,**info):\n",
    "    \"\"\"打印个人的信息\"\"\"\n",
    "    return name,info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "abdd73c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "build_person = person(\"Lihua\",age=18,job=\"student\",address=\"广州南方学院\",e_mail=\"123456@qq.com\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "5974a981",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('Lihua', {'age': 18, 'job': 'student', 'address': '广州南方学院', 'e_mail': '123456@qq.com'})\n"
     ]
    }
   ],
   "source": [
    "print(build_person)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1abbf7d2",
   "metadata": {},
   "source": [
    "### 2. 函数返回的结果\n",
    "> 1.函数可以返回任意类型的值，包括列表，字典和集合等较为复杂的数据结构  \n",
    ">> 1.函数返回一个集合  \n",
    ">> 2.函数返回一个字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "20ada991",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 返回值为一个集合\n",
    "def search4vowels(word):\n",
    "    \"\"\"Return a boolean based on any vowels found\"\"\"\n",
    "    vowels = set('aeiou')\n",
    "    found = vowels.intersection(set(word))\n",
    "    return found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "1c4c7ee1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'e', 'o', 'a', 'i'}\n"
     ]
    }
   ],
   "source": [
    "word = search4vowels('hello Zhichao')\n",
    "print(word)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "1148a2a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 返回值为一个字典\n",
    "def build_person(name,age,job):\n",
    "    \"\"\"返回一个字典，其中包含有关一个人的信息\"\"\"\n",
    "    person = {'name':name,'age':age,'job':job}\n",
    "    return person"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "fe954a63",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'Lihua', 'age': 18, 'job': 'student'}\n"
     ]
    }
   ],
   "source": [
    "person = build_person(\"Lihua\",18,\"student\")\n",
    "print(person)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e8987c2",
   "metadata": {},
   "source": [
    "# 3. 模块\n",
    "> 1. 模块 用户都不需要看底层代码\n",
    "> 2. 调用模块的方法  \n",
    ">> 1. 必须先调用模块，才可以使用  \n",
    ">>> 1. import moudle_name 直接调用整个模块，此时模块的所有函数处于备用状态，使用的方法为：moudle_name.func_name()  \n",
    ">>> 2. from moudle_name import func_name,此时，仅调用模块的某一个函数，只能使用这个模块的该函数，使用方法为 func_name()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "096daff5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
